#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_wifi_api.h"
#include "lwip/ip_addr.h"
#include "lwip/netifapi.h"
#include "lwip/sockets.h"
#include "MQTTClient.h"
#include "mqtt_test.h"
#include <MQTTConnect.h>
// 新增：包含led_control的头文件，调用其函数
#include "led_control.h"

static MQTTClient mq_client;
unsigned char *onenet_mqtt_buf;
unsigned char *onenet_mqtt_readbuf;
int buf_size;
Network n;
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
static int led_state = 0;  // 仅保留状态变量，函数调用led_control中的实现


// MQTT消息回调函数
void mqtt_callback(MessageData *msg_data) {
    LOS_ASSERT(msg_data);
    char msg_content[32] = {0};
    memcpy(msg_content, msg_data->message->payload, msg_data->message->payloadlen);

    printf("Serial <- Received Topic: %.*s, Message: %s\r\n",
           msg_data->topicName->lenstring.len,
           msg_data->topicName->lenstring.data,
           msg_content);

    // 调用led_control中的led_set_state函数
    char response[32] = {0};
    if (strcmp(msg_content, "turn-on") == 0) {
        led_set_state(1); // 调用led_control.c中的实现
        snprintf(response, sizeof(response), "灯的状态: 亮");
    } else if (strcmp(msg_content, "turn-off") == 0) {
        led_set_state(0); // 调用led_control.c中的实现
        snprintf(response, sizeof(response), "灯的状态: 灭");
    } else {
        snprintf(response, sizeof(response), "不支持的指令: %s", msg_content);
    }

    // 发布灯状态到接收主题
    MQTTMessage reply;
    reply.qos = 0;
    reply.retained = 0;
    reply.payload = (void *)response;
    reply.payloadlen = strlen(response);
    char recv_topic[] = "24iot01/team16/led/recv";
    if (MQTTPublish(&mq_client, recv_topic, &reply) == 0) {
        printf("Serial -> Sent Topic: %s, Message: %s\r\n", recv_topic, response);
    } else {
        printf("MQTT Publish Failed!\r\n");
    }
}

int mqtt_connect(void) {
    int rc = 0;
    // 调用led_control.c中的led_gpio_init函数
    led_gpio_init();  

    NetworkInit(&n);
    NetworkConnect(&n, "broker.emqx.io", 1883);  

    buf_size = 4096 + 1024;
    onenet_mqtt_buf = (unsigned char *)malloc(buf_size);
    onenet_mqtt_readbuf = (unsigned char *)malloc(buf_size);
    if (!(onenet_mqtt_buf && onenet_mqtt_readbuf)) {
        printf("No memory for MQTT buffer!\r\n");
        return -2;
    }

    MQTTClientInit(&mq_client, &n, 1000, onenet_mqtt_buf, buf_size, onenet_mqtt_readbuf, buf_size);
    MQTTStartTask(&mq_client);

    data.keepAliveInterval = 30;
    data.cleansession = 1;
    data.clientID.cstring = "team16";
    data.username.cstring = "team16";
    data.password.cstring = "123456";
    mq_client.defaultMessageHandler = mqtt_callback;

    rc = MQTTConnect(&mq_client, &data);
    if (rc != 0) {
        printf("MQTTConnect failed! rc=%d\r\n", rc);
        return rc;
    }
    printf("MQTTConnect success!\r\n");

    char send_topic[] = "24iot01/team16/led/send";
    MQTTSubscribe(&mq_client, send_topic, 0, mqtt_callback);
    printf("Subscribe Command Topic: %s\r\n", send_topic);

    return 0;
}

void mqtt_test(void) {
    mqtt_connect();
}